//
//  YMCaptureViewController.m
//  YMPAY
//
//  Created by yameng on 14-5-10.
//  Copyright (c) 2014年 YAMENG. All rights reserved.
//

#import "YMCaptureViewController.h"
#import "AGSimpleImageEditorView.h"

@interface YMCaptureViewController ()
{
    AGSimpleImageEditorView *editorView;
    UILabel *titleLabel;
    UIView *topView;
}
@end

@implementation YMCaptureViewController
@synthesize delegate;
@synthesize currentImage;

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    [editorView setImage:currentImage];
    [editorView setBackgroundColor:[UIColor yellowColor]];
    
    //self.view.backgroundColor = [CDUtil colorWithHexString:@"#EDEEF3"];
    
    //[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    
    self.navigationController.navigationBar.hidden = YES;
    
    if (topView == nil)
    {
        topView = [[UIView alloc] init];
    }
    topView.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, NAV_HEIGHT);
    topView.backgroundColor = kWhiteColor;
    [self.view addSubview:topView];
    
    UIView *redLine = [[UIView alloc] init];
    redLine.backgroundColor = kGreenColor;
    redLine.frame = CGRectMake(0.0f, NAV_HEIGHT-1.0f, kDeviceWidth, 1.0f);
    [topView addSubview:redLine];
    
    UIButton *headerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [headerBtn setBackgroundImage:[UIImage imageNamed:@"01back"] forState:UIControlStateNormal];
    [headerBtn setBackgroundImage:[UIImage imageNamed:@"01back"] forState:UIControlStateHighlighted];
    headerBtn.frame = CGRectMake(14.0, 20.0f+7.0f, 22, 22);
    [headerBtn addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    headerBtn.tag = 116;
    [topView addSubview:headerBtn];
    
    UIButton *filterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [filterBtn setTitle:@"完成" forState:UIControlStateNormal];
    [filterBtn setTitle:@"完成" forState:UIControlStateHighlighted];
    [filterBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    [filterBtn setTitleColor:kBlueColor forState:UIControlStateHighlighted];
    filterBtn.backgroundColor = [UIColor clearColor];
    filterBtn.titleLabel.font = kAppFont(Home_Big_Size);
    filterBtn.frame = CGRectMake(kDeviceWidth-14.0f/2.0f-120.0f/2.0f, 20.0f+7.0f, 120.0f/2.0f, 60.0f/2.0f);
    [filterBtn addTarget:self action:@selector(clickRight) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:filterBtn];
    
    
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 20, kDeviceWidth-40*2, 40)];
    titleLabel.text = @"图片";
    titleLabel.font = kAppFont(36/2);
    titleLabel.textColor = kRGB(119, 119, 121);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:titleLabel];

    UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [menuButton setBackgroundImage:[UIImage imageNamed:@"btn_back_n"] forState:UIControlStateNormal];
    [menuButton setBackgroundImage:[UIImage imageNamed:@"btn_back_n"] forState:UIControlStateHighlighted];
    [menuButton addTarget:self  action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    menuButton.frame = CGRectMake(10.0f, 30.0f, 27.0f, 27.0f);
    //UIBarButtonItem *revealButton = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
    //self.navigationItem.leftBarButtonItem = revealButton;
    //[topView addSubview:menuButton];
    
    UIButton *menuButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [menuButton1 setBackgroundImage:[UIImage imageNamed:@"feedback_right"] forState:UIControlStateNormal];
    [menuButton1 setBackgroundImage:[UIImage imageNamed:@"feedback_right"] forState:UIControlStateHighlighted];
    [menuButton1 addTarget:self  action:@selector(clickRight) forControlEvents:UIControlEventTouchUpInside];
    menuButton1.frame = CGRectMake(kDeviceWidth-50.0f, 30.0f, 27.0f, 27.0f);
    //UIBarButtonItem *revealmenuButton1 = [[UIBarButtonItem alloc] initWithCustomView:menuButton1];
    //self.navigationItem.rightBarButtonItem = revealmenuButton1;
    //[topView addSubview:menuButton1];
}

- (void)backButtonAction:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}

- (void)clickLeft {
    [self dismissViewControllerAnimated:YES completion:^{}];
}

- (void)clickRight {
    //output为截取后的图片，UIImage类型
    UIImage *resultImage = currentImage;//editorView.output;
    //通过代理回传给上一个界面显示
    [self.delegate passImage:resultImage];
    [self dismissViewControllerAnimated:YES completion:^{}];
    
}

#define TABLE_VIEW_RECT_II CGRectMake(0, NAV_HEIGHT, kDeviceWidth, kDeviceHeight-NAV_HEIGHT)
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //[self.navBar setFrame:NAV_RECT leftImage:kGoBackImage centerTitle:NSLocalizedString(@"navBar_centerTitle_image_cropping", nil)  rightLabel:NSLocalizedString(@"btn_save", nil)];
    ///[self.navBar addTarget:self LeftAction:@selector(clickLeft) RightAction:@selector(clickRight)];
    
    //image为上一个界面传过来的图片资源
    editorView = [[AGSimpleImageEditorView alloc] init];
    editorView.frame = TABLE_VIEW_RECT_II;
    
    //外边框的宽度及颜色
    //editorView.borderWidth = 1.f;
    //editorView.borderColor = [UIColor blackColor];
    
    //截取框的宽度及颜色
    editorView.ratioViewBorderWidth = 3.f;
    //editorView.ratioViewBorderColor = [UIColor orangeColor];
    
    //截取比例，我这里按正方形1:1截取（可以写成 3./2. 16./9. 4./3.）
    //editorView.ratio = 1./1.;//正方形
    //editorView.ratio = 5./3.;//正方形
    
    [self.view addSubview:editorView];
}

//- (void)setImage:(UIImage*)image{
//    [editorView setImage:image];
//    [editorView setBackgroundColor:[UIColor blackColor]];
//}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
